package club.itstudio.IContainer.service;

import club.itstudio.IContainer.entity.StudentCourseApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class StudentCourseApplicationService {
    private JdbcTemplate jdbcTemplate;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private StudentCourseApplicationMapper studentCourseApplicationMapper = new StudentCourseApplicationMapper();

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean insert(StudentCourseApplication application) {
        String sql = "insert into student_course_application(user_id, course_id, status) " +
                "values(?, ?, ?)";
        try {
            return jdbcTemplate.update(sql, application.userID, application.courseID, application.status) == 1;
        }catch (DataAccessException e) {
            logger.error("error insert new student course application", e);
            return false;
        }
    }

    public List<StudentCourseApplication> getByUserID(int userID) {
        String sql = "select * from student_course_application where user_id=?";
        return jdbcTemplate.query(sql, studentCourseApplicationMapper, userID);
    }

    public List<StudentCourseApplication> getByUserIDAndPage(int userID, int page, int limit) {
        String sql = "select * from student_course_application where user_id=? order by id desc limit ?,?";
        return jdbcTemplate.query(sql, studentCourseApplicationMapper, userID, (page - 1) * limit, limit);
    }

    public List<StudentCourseApplication> getByCourseID(int courseID) {
        String sql = "select * from student_course_application where course_id=?";
        return jdbcTemplate.query(sql, studentCourseApplicationMapper, courseID);
    }

    public StudentCourseApplication getByUserIDAndCourseID(int userID, int courseID) {
        String sql = "select * from student_course_application where user_id=? and course_id=?";
        StudentCourseApplication application = null;
        try {
            application = jdbcTemplate.queryForObject(sql, studentCourseApplicationMapper, userID, courseID);
        }catch (DataAccessException e) {
            logger.error("no such student course application  user id:" + userID + ", course id:" +  courseID);
        }
        return application;
    }

    public boolean updateStatus(StudentCourseApplication application)  {
        String sql = "update student_course_application set status=? where user_id=? and course_id=?";
        try {
            return jdbcTemplate.update(sql, application.status, application.userID, application.courseID) == 1;
        }catch (DataAccessException e) {
            logger.error("error update student course application status:", e);
            return false;
        }
    }
}

class StudentCourseApplicationMapper implements RowMapper<StudentCourseApplication> {
    @Override
    public StudentCourseApplication mapRow(ResultSet resultSet, int i) throws SQLException {
         StudentCourseApplication application = new StudentCourseApplication();
         application.id = resultSet.getInt("id");
         application.userID = resultSet.getInt("user_id");
         application.courseID = resultSet.getInt("course_id");
         application.status = resultSet.getInt("status");
         return application;
    }
}
